#ifndef _THREAD_H_
#define _THREAD_H_
#include <iostream>
#include <string>
#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <functional>

namespace ThreadModlue
{
    static uint32_t number = 1;

    template <typename T>
    class Thread
    {
        using func_t = std::function<void(T)>;

    private:
        void EnableDetach()
        {
            std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }

    public:
        Thread(func_t func, T data)
            : _tid(0),
              _isdetach(false),
              _isrunning(false),
              _res(nullptr),
              _func(func),
              _data(data)
        {
            _name = "THread-" + std::to_string(number++);
        }
        void Detach()
        {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            EnableDetach();
        }
        static void *Routine(void *args) // 类内的成员函数默认含有this指针
        {
            Thread<T> *self = static_cast<Thread<T> *>(args);
            self->EnableRunning();

            if (self->_isdetach)
                self->Detach();
            pthread_setname_np(self->_tid, self->_name.c_str());
            self->_func(self->_data);

            return nullptr;
        }
        void EnableRunning()
        {
            _isrunning = true;
        }
        bool Start()
        {
            if (_isrunning)
                return false;
            int n = pthread_create(&_tid, nullptr, Routine, this);
            if (n != 0)
            {
                std::cerr << "create thread error: " << strerror(n) << std::endl;
                return false;
            }
            else
            {
                std::cout << _name << "create success" << std::endl;
                return true;
            }
        }
        bool Stop()
        {
            if (_isrunning)
            {
                int n = pthread_cancel(_tid);
                if (n != 0)
                {
                    std::cerr << "create thread error: " << strerror(n) << std::endl;
                    return false;
                }
                else
                {
                    _isrunning = false;
                    std::cout << _name << "Stop" << std::endl;
                    return true;
                }
                _isrunning = false;
            }

            return false;
        }
        void Join()
        {
            if (_isdetach)
            {
                std::cout << "你的线程已经是Join了，不能在Join了" << std::endl;
                return;
            }
            int n = pthread_join(_tid, &_res);
            if (n != 0)
            {
                std::cerr << "create thread error: " << strerror(n) << std::endl;
            }
            std::cout << "Join success" << std::endl;
        }

        ~Thread()
        {
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *_res;
        func_t _func;
        T _data;
    };
}

#endif